package Easy;

// 35.搜索插入位置
public class Solution35 {
    public static int searchInsert(int[] nums, int target) {
        int i = 0, j = nums.length - 1;
        int k;
        while (i < j) {
            k = (i + j) / 2;
            if (target > nums[k]) {
                if (i != k)
                    i = k;
                else i = k + 1;
            } else if (target < nums[k]){
                if (j != k)
                    j = k;
                else j = k - 1;
            }
            else return k;
        }
        if (nums[i] < target)
            return i + 1;
        else return i;
    }

    public static void main(String[] args) {
        System.out.println(searchInsert(new int[]{1, 3, 5, 6}, 7));
    }
}
